package cn.mygweb.admin.modules.sys.service.impl;

import java.io.File;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.mygweb.admin.modules.sys.mapper.AttachmentMapper;
import cn.mygweb.admin.modules.sys.pojo.Attachment;
import cn.mygweb.admin.modules.sys.pojo.Menu;
import cn.mygweb.admin.modules.sys.service.AttachmentService;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/** 
* <p>Title: AttachmentServiceImpl.java</p>
* <p>Description: 附件管理业务层实现类</p>  
* @author gxb 
* @date 2019年8月5日 
* <p>Copyright: Copyright (c) 2019</p>
* <p>Company: www.mygweb.cn</p> 
*/
@Service
public class AttachmentServiceImpl implements AttachmentService {

	@Autowired
	private AttachmentMapper attachmentMapper;
	
	/* (non-Javadoc)
	 * @see cn.mygweb.admin.modules.sys.service.AttachmentService#select(cn.mygweb.admin.modules.sys.pojo.Attachment)
	 */
	@Override
	@Transactional(propagation=Propagation.SUPPORTS)
	public List<Attachment> selectList(Attachment attachment) {
		Example example = new Example(Menu.class);
		example.setOrderByClause("upload_time desc");
		return attachmentMapper.selectByExample(example);
	}

	/* (non-Javadoc)
	 * @see cn.mygweb.admin.modules.sys.service.AttachmentService#insert(cn.mygweb.admin.modules.sys.pojo.Attachment)
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void insert(Attachment attachment) {
		attachmentMapper.insertSelective(attachment);
	}

	/* (non-Javadoc)
	 * @see cn.mygweb.admin.modules.sys.service.AttachmentService#selectByIds(java.util.List)
	 */
	@Override
	@Transactional(propagation=Propagation.SUPPORTS)
	public List<Attachment> selectByIds(List<Object> idList) {
		Example example = new Example(Attachment.class);
		Criteria criteria = example.createCriteria();
		criteria.andIn("id", idList);
		return attachmentMapper.selectByExample(example);
	}

	/* (non-Javadoc)
	 * @see cn.mygweb.admin.modules.sys.service.AttachmentService#selectById(java.lang.String)
	 */
	@Override
	public Attachment selectById(String id) {
		return attachmentMapper.selectByPrimaryKey(id);
	}
	
	/* (non-Javadoc)
	 * @see cn.mygweb.admin.modules.sys.service.AttachmentService#deleteById(java.lang.String, boolean)
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void deleteById(String id, boolean delFile) {
		if(delFile) {
			String path = attachmentMapper.selectByPrimaryKey(id).getUploadPath();
			File file = new File(path);
			file.delete();
		}
		attachmentMapper.deleteByPrimaryKey(id);
	}

	/* (non-Javadoc)
	 * @see cn.mygweb.admin.modules.sys.service.AttachmentService#update(cn.mygweb.admin.modules.sys.pojo.Attachment)
	 */
	@Override
	public void update(Attachment attachment) {
		attachmentMapper.updateByPrimaryKeySelective(attachment);
	}
	
}
